﻿using System;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class _CadastroEmpresa : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnCadastro_Click(object sender, ImageClickEventArgs e)
    {
        if (!ClassesSiteIntranet.Validadores.ValidaCNPJ(txtCNPJ.Text))
        {
            Response.Write("<script>alert('CNPJ inválido'); history.go(-1);</script>");
        }
        else
        {
            if (EmpresaExiste(txtCNPJ.Text))
                Response.Redirect("mensagens.aspx?id=3");
            else
            {
                SqlConnection conexaoIntranet = new SqlConnection(ConfigurationManager.ConnectionStrings["conexao_intranet"].ConnectionString);
                SqlCommand insereEmpresa = null;
                try
                {
                    conexaoIntranet.Open();
                    string instrucao = "INSERT INTO CamaraEmpresas (CNPJ, RazaoSocial, NomeFantasia, SegmentoAtuacao, Endereco, Numero, Complemento, Bairro, Cidade, UF, CEP, DDDFone, Fone, Email, ResponsavelCadastro, Site, Senha) VALUES ('" + txtCNPJ.Text + "', '" + txtRazao.Text.Replace("'", "") + "', '" + txtNomeFantasia.Text.Replace("'", "") + "', '" + txtSegmentoAtuacao.Text.Replace("'", "") + "', '" + txtEndereco.Text.Replace("'", "") + "', '" + txtNumero.Text + "', '" + txtComplemento.Text.Replace("'", "") + "', '" + txtBairro.Text.Replace("'", "") + "', '" + txtCidade.Text.Replace("'", "") + "', '" + txtUF.Text + "', '" + txtCEP.Text + "', '" + txtFonePre.Text + "', '" + txtFone.Text + "', '" + txtEmail.Text + "', '" + txtResponsavelCadastro.Text.Replace("'", "") + "', '" + txtSite.Text.Replace("'", "") + "', '" + txtSenha.Text.Replace("'", "") + "')";
                    insereEmpresa = new SqlCommand(instrucao, conexaoIntranet);
                    insereEmpresa.ExecuteNonQuery();
                    Session["cnpj"] = txtCNPJ.Text;
                    Response.Redirect("mensagensc.aspx?id=2");
                }
                finally
                {
                    conexaoIntranet.Close();
                }
            }
        }
    }

    private bool EmpresaExiste(string cnpj)
    {
        bool jaCadastrado = false;
        SqlConnection conexaoIntranet = new SqlConnection(ConfigurationManager.ConnectionStrings["conexao_intranet"].ConnectionString.ToString());
        string instrucao = "SELECT cnpj FROM CamaraEmpresas WHERE cnpj = '" + cnpj + "'";
        try
        {
            if (conexaoIntranet.State != ConnectionState.Open)
                conexaoIntranet.Open();
            SqlCommand pesquisaEmpresa = new SqlCommand(instrucao, conexaoIntranet);
            SqlDataReader drEmpresa = pesquisaEmpresa.ExecuteReader();
            jaCadastrado = drEmpresa.HasRows;
            drEmpresa.Close();
        }
        finally
        {
            conexaoIntranet.Close();
        }
        return jaCadastrado;
    }

    protected void txtCEP_TextChanged(object sender, EventArgs e)
    {
        if (txtCEP.Text.Length == 8)
        {
            SiteBLL.CEPS api = new SiteBLL.CEPS();
            DataTable tabela = api.GetLogradouroPorCEP(txtCEP.Text, "cep", "pe");
            if (tabela.Rows.Count > 0)
            {
                DataRow linha = tabela.Rows[0];
                string endereco = linha["descricaologradouro"].ToString();
                int posicao = endereco.IndexOf(" - at");
                if (posicao > 0)
                    endereco = endereco.Substring(0, posicao);
                else
                {
                    posicao = endereco.IndexOf(" - de ");
                    if (posicao > 0)
                        endereco = endereco.Substring(0, posicao);
                }
                txtEndereco.Text = endereco.ToString().ToUpper();
                txtBairro.Text = linha["descricaobairro"].ToString().ToUpper();
                txtCidade.Text = linha["descricaocidade"].ToString().ToUpper();
                txtUF.Text = linha["ufsigla"].ToString().ToUpper();
                txtNumero.Text = "";
                txtComplemento.Text = "";
                txtNumero.Focus();
            }
        }
    }
}